#include <stdio.h>
#include <string.h>
#include "cJSON.h"

int main(int argc, char const *argv[])
{
    //创建对象
    cJSON* object =  cJSON_CreateObject();
    
    //添加键值对
    //创建子对象
    cJSON* subobj = cJSON_CreateObject();
    cJSON_AddItemToObject(subobj, "factory", cJSON_CreateString("一汽大众"));
    cJSON_AddItemToObject(subobj, "last", cJSON_CreateNumber(31));
    cJSON_AddItemToObject(subobj, "price", cJSON_CreateNumber(83));
    cJSON_AddItemToObject(subobj, "sell", cJSON_CreateNumber(49));
    cJSON_AddItemToObject(subobj, "sum", cJSON_CreateNumber(80));

    cJSON* arr = cJSON_CreateArray();
    cJSON_AddItemToArray(arr, cJSON_CreateNumber(123));
    cJSON_AddItemToArray(arr, cJSON_CreateBool(1));
    cJSON_AddItemToArray(arr, cJSON_CreateString("hello, world!"));
    cJSON* subsub = cJSON_CreateObject();
    cJSON_AddItemToObject(subsub, "梅赛德斯奔驰", cJSON_CreateString("心所向, 持以恒"));
    cJSON_AddItemToArray(arr, subsub);
    cJSON_AddItemToObject(subobj, "other", arr);

    cJSON_AddItemToObject(object, "奔驰", subobj);

    //格式化为字符串
    char* data  = cJSON_Print(object);
    printf("json data : %s \n", data);

    FILE* fp = fopen("car2.json", "w");
    fwrite(data, sizeof(char), strlen(data), fp);
    fclose(fp);
    cJSON_Delete(object);
    return 0;
}
